var flag=false; //是否登录
$(function () {
    getCookie();
});
function getCookie(){
    var strcookie = document.cookie;//获取cookie字符串
    var arrcookie = strcookie.split(";");//分割
//遍历匹配
    for ( var i = 0; i < arrcookie.length; i++) {
        var arr = arrcookie[i].split("=");
        if (arr[0] == 'UserName'){
            if (arr[1] != "" && arr[1] != undefined ) {
                flag=true;
                replaceUser(arr[1]);
                $("#dropMenu").hide();
            }
        }
    }
    return "";
}
$('#userNameLogin').click(function () {
    if (flag ==true){
        $("#quitLogin").toggle();
    }
});
$('#quit').click(function () {
    if (flag ==true){
        $.ajax({
            url: _server + "/bssp/user/quit",
            type: 'post',
            dataType: 'json',
            data: {},
            xhrFields: {
                withCredentials: true
            },
            success: function (result) {
                if (result.code==1){
                    layer.msg(result.data,{time:1500},function () {
                       window.location.href="http://localhost:8085/bssp/main";
                    });
                }else{
                    layer.alert(result.data);
                }
            },
            error: function () {
                layer.msg("请求失败",{time:1500});
            }
        });
    }
});

function replaceUser(Name) {
    $('#userName').text(Name);
}
$('#Login').click(function () {
    var Name=$('#mainName').val();
    if (Name.length <= 0) {
        $('#mainName').attr("placeholder", "用户名不能为空 ！");
        return false;
    }
    var Password=$('#mainPassword').val();
    if (Password.length <= 0) {
        $('#mainPassword').attr("placeholder", "密码不能为空 ！");
        return false;
    }
   $.ajax({
        url: _server + "/bssp/user/login",
        type: 'post',
        dataType: 'json',
        data: {"Name":Name,"Password":Password},
        xhrFields: {
            withCredentials: true
        },
        success: function (result) {
            if (result.code==1){
                layer.msg("登陆成功",{time:1500},function () {
                    $('#myModal').modal('hide');
                    $('#userName').text(result.data.name);
                    $("#dropMenu").hide();
                    window.location.href="http://localhost:8085/bssp/main"
                });

            }else{
                layer.alert(result.data);
            }
        },
        error: function () {
            layer.msg("请求失败",{time:1500});
        }
    });
});